<?php
declare (strict_types = 1);

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use app\common\model\system\Menu as MenuModel;
use think\Exception;
use think\facade\Db;

class Menu extends Command
{

    const TYPE_INDEX = 'index';
    const TYPE_ADMIN = 'admin';

    protected function configure()
    {
        // 指令配置
        $this->setName('menu')
            ->setDescription('initialization menu command');

        // 指令配置
        $this->setName('menu')
            ->addArgument('action', Argument::OPTIONAL, "[init] init menus")
            ->setDescription('Initialization menus');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');
        $action = $action ?: 'init';

        switch ($action){
            case 'init':
                $this->initMenus($output);
                break;
            default:
                $output->writeln('action param error.');
                break;
        }





    }

    /**
     * 初始化菜单
     * @param Output $output
     * @return bool
     */
    private function initMenus(Output $output){
        //添加数据
        $menus = config('menus.init');
        if (empty($menus)){
            $output->writeln("Menus data is empty.");
            return false;
        }

        //事务
        Db::transaction(function () use ($menus) {
            //先清空菜单数据
            $res = MenuModel::destroy(function ($query){
                return true;
            });
            if( $res === false)
            {
                throw new Exception('清除源数据失败.');
            }

            foreach ($menus as $k => $v){

                //明确类型
                if($k === static::TYPE_INDEX){
                    $type = MenuModel::TYPE_INDEX;
                }else{
                    $type = MenuModel::TYPE_ADMIN;
                }

                //遍历菜单每一项
                foreach ($v as $kk => $vv){
                    $data = [
                        'name' => $vv['name'],
                        'route' => $vv['route'],
                        'parent_id' => 0,
                        'type' => $type,
                        'sort' => $kk + 100,
                        'icon' => '&#xe66c;'
                    ];
                    $model = new MenuModel();
                    if ($model->save($data) === false){
                        throw new Exception('添加菜单失败.');
                    }

                    if (isset($vv['children']) && !empty($vv['children'])){
                        $children = [];
                        foreach ($vv['children'] as $kkk => $vvv){
                            $children[] = [
                                'name' => $vvv['name'],
                                'route' => $vvv['route'],
                                'parent_id' => $model->id,
                                'type' => $model->type,
                                'sort' => $kkk + 100,
                                'icon' => '&#xe66c;'
                            ];
                        }

                        //保存子菜单
                        (new MenuModel())->saveAll($children);
                    }
                }
            }
        });




        $output->writeln("initialization menus success");
        return true;
    }
}
